/**
 * Created by wenchuan on 2017/9/4.
 */
(function () {

    var self,
        map,
        locations = []
    ;

    function Map() {
        self = this;

        self.init();
    }

    Map.prototype.init = function () {
        app.tag_city = '_tag_city';
        var geolocation;
        var _lng = app.lStorage.get(app.tag_longitude);
        var _lat = app.lStorage.get(app.tag_latitude);
        if (!_lng) {
            //_lng = 120.296474;
            //_lat = 30.444385;
            map = new AMap.Map('container', {
                zoom: 14,
                //center: [_lng, _lat],
                resizeEnable: true
            });
            geolocationFun();
        } else {
            //mapContainer
            //AMap.convertFrom([_lng, _lat], 'gps', function (status, info) {
            //    if (info.info === 'ok') {
             //       var _lnglat = info.locations[0].c;
                    map = new AMap.Map('container', {
                        zoom: 14,
                        center: [_lng, _lat],
                        resizeEnable: true
                    });
             //   }
            //});

            setTimeout(function () {
                geolocationFun();
            }, 800);
        }

        //浏览器定位
        function geolocationFun(cb) {
            map.plugin('AMap.Geolocation', function () {
                geolocation = new AMap.Geolocation({
                    enableHighAccuracy: false,//是否使用高精度定位，默认:true
                    maximumAge: 600,//缓存10分钟
                    showCircle: false,
                    noIpLocate: 0,//1: 手机设备禁止使用IP定位
                    buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
                    zoomToAccuracy: false     //定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
                    //buttonPosition: 'RB'
                });
                geolocation.getCurrentPosition();
                // map.addControl(geolocation);
                AMap.event.addListener(geolocation, 'complete', function (e) {
                    //TODO
                    _lng = e.position.lng;
                    _lat = e.position.lat;
                    //if (cb) {
                    //    placeSearchFun();
                    //    cb(e);
                    //}
                    //document.getElementById('msgBox').style.display = 'none';
                    //限制城市
                    //if (mAutocomplete) {
                    //mAutocomplete.setCity(e.addressComponent.citycode);
                    //}
                    app.lStorage.set(app.tag_latitude, _lat);
                    app.lStorage.set(app.tag_longitude, _lng);
                    app.lStorage.set(app.tag_city, e.addressComponent.citycode);
                });
                AMap.event.addListener(geolocation, 'error', function (e) {
                    //alert('地图定位出错，请重新进入页面');
                    //geolocation.getCurrentPosition();
                    // mGeocoder.getAddress([fromObj.location.lng, fromObj.location.lat], function (status, result) {
                    //     if (status === 'complete' && result.info === 'OK') {
                    //         var dobj = result.regeocode.addressComponent;
                    //         mAddAddressData.PROVINCE_NAME = fromObj.pname || dobj.province;
                    //         mAddAddressData.CITY_NAME = fromObj.cityname;
                    //     }
                    // });
                });
            });
        }

        setTimeout(function () {
            self.getData();
        }, 1500);

        setInterval(function () {
            self.getData();
        }, 1000 * 10 * 3);
    };

    Map.prototype.getData = function () {
        app.ajax({
            xurl: "api/wx/gwa/getLocations",
            cb: function (result) {
                if (result.ok) {
                    result.data.data.forEach(function (v) {
                        if (v.GWAA019) {
                            var _items = locations.filterKV({GWAA001: v.GWAA001});
                            if (_items.length > 0) {
                                var _item = _items[0];
                                if (_item.GWAA019 != v.GWAA019 || _item.GWAA020 != v.GWAA020) {
                                    _item.GWAA020 = v.GWAA020;
                                    _item.GWAA019 = v.GWAA019;
                                    _item.marker.setPosition([v.GWAA019, v.GWAA020]);
                                }
                            } else {
                                //AMap.convertFrom([v.GWAA019, v.GWAA020], 'gps', function (status, info) {
                                //    if (info.info === 'ok') {
                                        v.marker = new AMap.Marker({
                                            //position: info.locations[0],// 经纬度对象，也可以是经纬度构成的一维数组[116.39, 39.9]
                                            position: new AMap.LngLat(v.GWAA019, v.GWAA020),
                                            title: v.GWAA002,
                                            draggable:true
                                        });

                                        // 自定义点标记内容
                                        var markerContent = document.createElement("div");

                                        // 点标记中的图标
                                        var markerImg = document.createElement("img");
                                        markerImg.className = "markerlnglat";
                                        markerImg.src = "http://webapi.amap.com/theme/v1.3/markers/n/mark_b.png";
                                        markerContent.appendChild(markerImg);

                                        // 点标记中的文本
                                        var markerSpan = document.createElement("span");
                                        markerSpan.className = 'marker';
                                        markerSpan.innerHTML = v.GWAA002;
                                        markerContent.appendChild(markerSpan);
                                        v.marker.setContent(markerContent);
                                        map.add(v.marker);
                                        locations.push(v);
                               //     }
                               // });

                            }
                        }
                    });
                }
            }
        });
    };

    var mMap = new Map();

    if (typeof exports == "object") {
        module.exports = mMap;
    } else if (typeof define == "function" && define.amd) {
        define([], function () {
            return mMap;
        });
    } else {
        window.init = function () {
            mMap.init();
        };
    }
})();


